﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.App_Code;
using QXBook.Model;
using QXBook.Utils;
using QXBook.Service;

namespace Web.User
{
    public partial class MyAccount : UserBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //检测用户登录
            ServiceFactory.CreateInstance<UsersService>().CheckUserLogin();
            Title = "修改密码";
            if (!IsPostBack)
            {
                this.DataBind();
            }

            btnSave.Click += new EventHandler(btnSave_Click);
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void DataBind()
        {
            UsersInfo user = this.userInfo;
            litUserName.Text = user.UserName;
            iRelName.Value = user.RealName;
            iCardCode.Value = user.CardCode;
            iAddress.Value = user.Address;
            iQQ.Value = user.QQ;
            iMSN.Value = user.MSN;
            iTelphone.Value = user.Telphone;
            iEmal.Value = user.Email;
        }

        private UsersInfo GetModel(ref UsersInfo model)
        {
            string relname = CommonUtils.Post("iRelName");
            string cardcode = CommonUtils.Post("iCardCode");
            string address = CommonUtils.Post("iAddress");
            string qq = CommonUtils.Post("iQQ");
            string msn = CommonUtils.Post("iMSN");
            string telphone = CommonUtils.Post("iTelphone");
            string email = CommonUtils.Post("iEmal");

            model.RealName = relname;
            model.CardCode = cardcode;
            model.Address = address;
            model.QQ = qq;
            model.MSN = msn;
            model.Telphone = telphone;
            model.Email = email;
            return model;
        }

        private void Save()
        {
            string rtmessage = string.Empty;
            string resultMsg = string.Empty;
            try
            {
                UsersInfo user = this.userInfo;
                user = GetModel(ref user);

                rtmessage = ServiceFactory.CreateInstance<UsersService>().UpdateUserInfo(user);
                resultMsg = "管理员信息修改成功!";
            }
            catch (Exception ex)
            {
                base.AjaxErch("{state:false,message:\"" + CommonUtils.JSEncode(ex.Message.ToString()) + "\"}");
                return;
            }

            base.ResponseErch(rtmessage, resultMsg);
        }
    }
}